﻿$(function(){
	$('.img-box img').lazyload();
	
	var imgShowInterval = null;
	(function(){
		var currentIndex = 0;
		var maxImgNum = 10;
		//图片展示
		$('.img-slider .left-point').live(
			'click',
			function(){
				if( currentIndex-1 > 0 ){
					currentIndex -= 1;
				}else{
					currentIndex = maxImgNum-1;
				}
				var nextIndex = currentIndex%10;
				var marginLeftVal = nextIndex*955;
				$('#imgSliderUl').animate({marginLeft: '-' + marginLeftVal + 'px'});
				$('#sliderDots > a').removeClass('slider-on')
									 .eq(currentIndex).addClass('slider-on');
			}
		);
		$('.img-slider .right-point').live(
			'click',
			function(){
				if( currentIndex + 1 < maxImgNum ){
					currentIndex += 1;
				}else{
					currentIndex = 0;
				}
				var nextIndex = currentIndex%10;
				var marginLeftVal = nextIndex*955;
				$('#imgSliderUl').animate({marginLeft: '-' + marginLeftVal + 'px'});
				$('#sliderDots > a').removeClass('slider-on')
				 					 .eq(currentIndex).addClass('slider-on');
			}
		);
		
		imgShowInterval =  setInterval('playImgShow()',5000);
		
	})();
	
	
	
	var type = $("#ustypeerId").val();
	 
	if(type<6){
	   showUserDiv();
	}
   
	if(type>5){
		showOtherDiv();
	}
});


function playImgShow(){
	$('.img-slider .right-point').click();
}


$('.img-link').live(
	'mouseenter',
	function(){
		$(this).children('.overlay')
			   .animate({'top':'-52px'});
	}
).live(
	'mouseleave',
	function(){
		$(this).children('.overlay')
			   .animate({'top':'0'});
	}
);

// 选择性别方式
var sex=1;
$('#man').live(
	'click',
	function(){
	sex =1;
	}
);

$('#woman').live(
	'click',
	function(){
	sex =0;
	}
);


function canSubmit(){
var nickName = $("#nickName").val();
var email = $("#username").val();
var password = $("#password").val();
var passwordConfirm = $("#passwordConfirm").val();
var wPassword = $("#wPassword").val();
	
if(wPassword!="01234"){
	alert("请填写正确验证码！");
	return false;}
	
if(password=="" || password==null){
	return false;
	alert("密码不能为空");
}
if(email=="" || email==null){ 
    alert("用户名不能为空");
	return false;
}
if(password!=passwordConfirm){
	return false;
}
if(flag==1){
	return false;
}	

}

var flag=0;

function validateEmael(){
var username = $("#username").val();
    	$.ajax({
			url:"/auth/checkUser.action?account=" + username,
			type:"POST",
			dataType:"json",
			success: function(data){
				if(data.data){
				$("#input_email").show();
			  $("#input_email_s").html("该账号已经被注册！！");
			  flag=1;
    				return false;
				}else if(!data.data){
					$("#input_email").show();
					flag=0;
				$("#input_email_s").html("恭喜你，可以注册此账号！");
				}
			},
			error:function(){
				return false;
			}
		});
}

function validatePassword(){
var password = $("#password").val();
var passwordConfirm = $("#passwordConfirm").val();
if(password==passwordConfirm){
$("#input_passwordConfirm").show();
$("#error_comfirm").html("密码输入成功！");
return true;}
if(password!=passwordConfirm){

$("#input_passwordConfirm").show();
$("#error_comfirm").html("两次密码不一致！");
return false;}
}


function searchUser(d){
$.ajax({
			url:"/home/find.action?userId=" + d,
			type:"POST",
			dataType:"json",
			success: function(data){
			
		     	$("#userId").val(data.data.userId);
				$("#username").html(data.data.username);
				$("#realname").val(data.data.realname);
				$("#userType").val(data.data.type);
				var sexx = data.data.sex;
				if(sexx==1){
				$("#women").attr("checked","");
				$("#sex").attr("checked","checked");
				}
				if(sexx==0){
		        	$("#sex").attr("checked","");
				$("#women").attr("checked","checked");
				}
				$("#password").val(data.data.password);
				$("#headpic").val(data.data.headPic);
				$("#introduce").val(data.data.introduce);
				$("#message1").val(data.data.profile1);
				$("#message2").val(data.data.profile2);
				$("#message3").val(data.data.profile3);
				$("#message4").val(data.data.profile4);
				$("#message5").val(data.data.profile5);
				$("#message6").val(data.data.profile6);
				$("#message7").val(data.data.profile7);
			},
			error:function(){
				return false;
			}
		});
}
// 更新
$("#updateButtrn").live(
  'click',
  function(){
     			 var userId = $("#userId").val();
				 var username = $("#username").text();
				 var realname = $("#realname").val();
				 var userType = $("#userType").val();
				 var password = $("#password").val();
				 var headpic = $("#headpic").val();
				 var introduce =  $("#introduce").val();
				 var message1 = $("#message1").val();
				 var message2 = $("#message2").val();
				 var message3 = $("#message3").val();
				 var message4 = $("#message4").val();
				 var message5 = $("#message5").val();
				 var message6 = $("#message6").val();
				 var message7 = $("#message7").val();
	param = {
	  "userId":userId,
	  "username":username,
	  "realname":realname,
	  "userType":userType,
	  "password":password,
	  "headpic":headpic,
	  "introduce":introduce,
	  "message1":message1,
	  "message2":message2,
	  "message3":message3,
	  "message4":message4,
	  "message5":message5,
	  "message7":message7,
	  "message6":message6
	
	};
  $.ajax({
			url:"/home/update.action" ,
			type:"POST",
			data:param,
			dataType:"json",
			success: function(data){}
			});
  
    });
    
 // 删除用户
 $("#deleteButtrn").live(
  'click',
  function(){
     			 var userId = $("#userId").val();
     			if(userId==null||userId==""){
     			return false;}
  $.ajax({
			url:"/home/delete.action?userId="+userId ,
			type:"POST",
		
			dataType:"json",
			success: function(data){
			}
			});
				alert(删除用户成功);
			$("#userId").val(data.data.userId);
				$("#username").html("");
				$("#realname").val("");
				$("#userType").val("");
				$("#password").val("");
				$("#headpic").val("");
				$("#message1").val("");
				$("#message2").val("");
				$("#message3").val("");
				$("#message4").val("");
				$("#message5").val("");
				$("#message6").val("");
				$("#message7").val("");
  
    });
  // 后台显示其它的详情
function searchOther(d){
$.ajax({
			url:"/other/find.action?otherId=" + d,
			type:"POST",
			dataType:"json",
			success: function(data){
			
		     	$("#otherId").val(data.data.otherId);
				$("#othername").val(data.data.othername);	
				$("#otherType").val(data.data.type);
				$("#url").val(data.data.url);
				$("#introduce").val(data.data.introduce);
				$("#oMessage1").val(data.data.message1);
				$("#oMessage2").val(data.data.message2);
				$("#oMessage3").val(data.data.message3);
				$("#oMessage4").val(data.data.message4);
				$("#oMessage5").val(data.data.message5);
				$("#oMessage6").val(data.data.message6);
				$("#oMessage7").val(data.data.message7);
			},
			error:function(){
				return false;
			}
		});
}
// 更新其它类
$("#updateOtherButtrn").live(
  'click',
  function(){
     			 var otherId = $("#otherId").val();
				 var othername = $("#othername").val();
				 var type = $("#otherType").val();			
				 var url = $("#url").val();
				 var introduce = $("#introduce").val();
				 var oMessage1 = $("#oMessage1").val();
				 var oMessage2 = $("#oMessage2").val();
				 var oMessage3 = $("#oMessage3").val();
				 var oMessage4 = $("#oMessage4").val();
				 var oMessage5 = $("#oMessage5").val();
				 var oMessage6 = $("#oMessage6").val();
				 var oMessage7 = $("#oMessage7").val();
	param = {
	  "otherId":otherId,
	  "othername":othername,
	  "type":type,
	  "url":url,
	  "introduce":introduce,
	  "message1":oMessage1,
	  "message2":oMessage2,
	  "message3":oMessage3,
	  "message4":oMessage4,
	  "message5":oMessage5,
	  "message6":oMessage6,
	  "message7":oMessage7
	
	};
  $.ajax({
			url:"/other/update.action" ,
			type:"POST",
			data:param,
			dataType:"json",
			success: function(data){}
			});
  
    });
    
 // 删除其它类
 $("#deleteOtherButtrn").live(
  'click',
  function(){
     
     			var otherId = $("#otherId").val();
     			if(otherId==null||otherId==""){
     			return false;
     			}
  $.ajax({
			url:"/other/delete.action?otherId="+otherId ,
			type:"POST",
			dataType:"json",
			success: function(data){
			}
			});
				alert(删除成功);
			$("#otherId").val("");
				$("#othername").val("");
				$("#otherType").val("");
				$("#url").val("");
				$("#introduce").val("");
				$("#oMessage1").val("");
				$("#oMessage2").val("");
				$("#oMessage3").val("");
				$("#oMessage4").val("");
				$("#oMessage5").val("");
				$("#oMessage6").val("");
				$("#oMessage7").val("");
   
    });
    
  // 添加其它类
  $("#addOtherButtrn").live(
  'click',
  function(){
     			 
				 var othername = $("#othername").val();
				 var type = $("#otherType").val();
				 var introduce = $("#introduce").val();
				 var url = $("#url").val();
				 var oMessage1 = $("#oMessage1").val();
				 var oMessage2 = $("#oMessage2").val();
				 var oMessage3 = $("#oMessage3").val();
				 var oMessage4 = $("#oMessage4").val();
				 var oMessage5 = $("#oMessage5").val();
				 var oMessage6 = $("#oMessage6").val();
			     var oMessage7 = $("#oMessage7").val();
	param = {
	  "introduce":introduce,
	  "othername":othername,
	  "type":type,
	  "url":url,
	  "message1":oMessage1,
	  "message2":oMessage2,
	  "message3":oMessage3,
	  "message4":oMessage4,
	  "message5":oMessage5,
	  "message6":oMessage6,
	  "message7":oMessage7
	
	};
  $.ajax({
			url:"/other/add.action" ,
			type:"POST",
			data:param,
			dataType:"json",
			success: function(data){}
			});
  
    });
    
 
  //主页user与other 切换
function  showUserDiv(){
	$("#userDiv").attr("style","display:block");
	$("#otherDiv").attr("style","display:none");
}
  
function  showOtherDiv(){
	$("#userDiv").attr("style","display:none");
	$("#otherDiv").attr("style","display:block");
}

//news 编辑
$('.news-edit').live(
	'click',
	function(){
		window.location.hash = '';
		window.location.hash = 'edit';
		
		var newsId = $(this).attr('newsid');
		
		$.get(
			'/news/find',
			{
				'newsId': newsId
			},
			function(data){
				$('#edit-title').val(data.data.name);
				$('#edit-pic').val(data.data.picurl);
				$('#edit-id').val(data.data.newsId);
				$("#edit-message").val(data.data.absMessage);
				nicEditors.findEditor('edit-content').setContent(data.data.message);
				
			}
		);
		
	}
);



// 更新新闻
$("#saveNews").live(
  'click',
  function(){
     			 var newsId = $("#edit-id").val();
				 var picurl = $("#edit-pic").val();
				 var name   = $("#edit-title").val();
				 var message= nicEditors.findEditor('edit-content').getContent();
				 var absMessage = $("#edit-message").val();
				 
	param = {
	  "newsId":newsId,
	  "newsname":name,
	  "headpic":picurl,
	  "absMessage":absMessage,
	  "message":message
	};
  $.ajax({
			url:"/news/update.action" ,
			type:"POST",
			data:param,
			dataType:"json",
			success: function(data){
			alert("操作成功！");}
			});
  
 });
    
// 创建新闻
$("#addNews").live(
  'click',
  function(){
				 var picurl = $("#edit-pic").val();
				 var name   = $("#edit-title").val();
				 var message= nicEditors.findEditor('edit-content').getContent();
				 var absMessage = $("#edit-message").val();
				 
	param = {
	  "newsname":name,
	  "headpic":picurl,
	  "absMessage":absMessage,
	  "message":message
	};
  $.ajax({
			url:"/news/add.action" ,
			type:"POST",
			data:param,
			dataType:"json",
			success: function(data){
			alert("操作成功！");}
			});
  
 });
//检验优品密码
function validateWPassword(){
 	var wPassword = $("#wPassword").val();
	if(wPassword=="01234"){
	$("#wpasswordConfirm").show();
	$("#wpassword_comfirm").html("验证成功！");
	return true;}
	if(wPassword!="01234"){
	$("#wpasswordConfirm").show();
	$("#wpassword_comfirm").html("请联系优品管理员!");
	return true;}
}

$('.ad-container .ad-box').live(
	'mouseover',
	function(){
		$(this).animate({'opacity':0.6},{duration:100,queue:false});
	}
).live(
	'mouseout',
	function(){
		$(this).animate({'opacity':1},{duration:100,queue:false});
	}
);

$('.nav-menu a').live(
 		'mouseover',
 		function(){
 			$(this).animate({top:'-49px',height:'98px'},{duration:200,queue:false});
 		}
).live(
	'mouseout',
	function(){ var stype = $("#typestr").val();
                         var title = $(this).attr("id");
                          if(stype!=title ){
		$(this).animate({top:'0px',height:'49px'},{duration:200,queue:false});}
	}
);

$('#back-btn').live(
	'click',
	function(){
		$('html, body').animate({scrollTop: 0},500);
	}
);

$('#current-area-box').live(
	'mouseenter',
	function(){
		$(this).children('.opt').show(100);
	}
).live(
	'mouseleave',
	function(){
		$(this).children('.opt').hide(100);
	}
);

$('#current-area-box .opt> li').live(
	'click',
	function(){
		var area = $(this).text();
		$('#current-area-box .cur').text(area);
	}
);